<?php

class Serahterima_ppk extends CI_Controller {

    function __construct()
    {
        parent::__construct();			

        //	$userdata = array ('userLogin' => $userLogin,'logged_in' => TRUE,'groupId'=>$this->sys_login_model->groupId,'fullName'=>$this->sys_login_model->fullName,'userId'=>$this->sys_login_model->userId,'groupLevel'=>$this->sys_login_model->level);

        $this->load->model('/security/sys_menu_model');
        $this->load->model('/transaksi/spb_model');
        $this->load->model('/transaksi/serahterima_ppk_model');
        $this->load->model('/rujukan/spb_kategori_model');
        $this->load->model('/rujukan/bidang_model');
        $this->load->model('/rujukan/kegiatan_model');
        $this->load->model('/admin/user_model');
        $this->load->library("utility");

    }

    function index(){
        $data['title'] = 'Serah/Terima Dokumen ke PPK';		
        $data['objectId'] = 'serahterimaPPK';
        $data['tipeapproval'] = 'penguji';/// daftar spby yg muncul hanya yg sudah diverifikasi
        $data['bidanglist'] = $this->bidang_model->getListBidang($data['objectId']);
        $data['bidanglistFilter'] = $this->bidang_model->getListBidangFilter($data['objectId']);
        $data['kategorilist'] = $this->spb_kategori_model->getListKategori($data['objectId']);
        $data['kategorilistFilter'] = $this->spb_kategori_model->getListKategoriFilter($data['objectId']);
        $data['tipePeriode'] = $this->spb_model->get_filter_periode_type();
        $this->load->view('transaksi/serahterima_ppk_v',$data);
    }
	
	
	  public function add(){
        $data['title'] = 'Serah/Terima PPK';	
        $data['objectId'] = "addSerahTerima"; 
        $data['editmode'] = false;
        //$data['tipetandaterima'] = 'draft';
        $data['serahterima_id'] = '0';
        //$data['bidanglist'] = $this->bidang_model->getListBidang($data['objectId']);
        $this->load->view('transaksi/serahterima_rec_v',$data);
    }
    
    
    function grid($tipe,$periodeawal,$periodeakhir,$bidang,$kategori,$nomor,$tipeperiode){	
        $periodeawal = $this->utility->ourDeFormatSQLDate($periodeawal);
	$periodeakhir = $this->utility->ourDeFormatSQLDate($periodeakhir);
        echo $this->serahterima_ppk_model->easyGrid(1,$tipe,$periodeawal,$periodeakhir,$bidang,$kategori,$nomor,$tipeperiode);
    }
    
	 function griddetail($tipe,$serahterima_id){	        
        echo $this->serahterima_ppk_model->easyGridDetail(1,$tipe,$serahterima_id);
    }
   
	function gridspb($tipe,$serahterima_id=null){	
        if (($serahterima_id!=null)&&($serahterima_id>0)){
            echo $this->serahterima_ppk_model->easyGridDetail(1,$tipe,$serahterima_id,true);
        }
        else{
            echo $this->serahterima_ppk_model->easyGridInput(1,$tipe,$serahterima_id);
        }
                
        
    }
	function get_new_number($bidang_id,$tahun){
        
        echo $this->spb_model->getNewNumber($bidang_id,$tahun);
    }
	
   private function get_form_values() {
            // XXS Filtering enforced for user input
        $data['nomor'] = $this->input->post("nomor", TRUE);
        $data['serahterima_id'] = $this->input->post("serahterima_id", TRUE);
        $data['tanggal'] = $this->input->post("tanggal", TRUE);
        $data['tipe'] = $this->input->post("tipe", TRUE);
        $data['keterangan'] = $this->input->post("keterangan", TRUE);               
        $data['spb_ids'] = $this->input->post("spb_ids", TRUE);
    
			$data['penyerah']= $this->input->post("penyerah", TRUE);
			$data['penerima']= $this->input->post("penerima", TRUE);
	//		$data['penerima']=$this->session->userdata('full_name');
		
        return $data;
    }

    
    
    function save($aksi="", $kode=""){
        $this->load->library('form_validation');
        $data = $this->get_form_values();
        $status = "";
        $result = false;
	//	var_dump($kode);die;
        //validasi form
        $this->form_validation->set_rules("nomor", 'Nomor Serah/Terima', 'trim|required|xss_clean');
        
        
        //$this->form_validation->set_rules("jumlah", 'Jumlah Uang', 'trim|required|xss_clean');

        $data['pesan_error'] = '';
        if ($this->form_validation->run() == FALSE){
                //jika data tidak valid kembali ke view
                $data["pesan_error"].=(trim(form_error("nomor"," "," "))==""?"":form_error("nomor"," "," ")."<br/>");
                //$data["pesan_error"].=(trim(form_error("jumlah"," "," "))==""?"":form_error("jumlah"," "," ")."<br/>");
                $status = $data["pesan_error"];
        }else {
            if($aksi=="add"){ // add
                if (!$this->serahterima_ppk_model->isExistKode($data['nomor'])){
                        $result = $this->serahterima_ppk_model->InsertOnDb($data,$status,$kode);
                }
                else
                        $data['pesan_error'] .= 'Nomor sudah ada';

            }else { // edit
                $result=$this->serahterima_ppk_model->UpdateOnDb($data,$kode);
                $data['pesan_error'] .= 'Update : '.$kode;
            }
            $data['pesan_error'] .= $status;	
        }

        if ($result){
                echo json_encode(array('success'=>true,"serahterima_id"=>$kode));
        } else {
                echo json_encode(array('msg'=>$data['pesan_error']));
        }
//		echo $status;

    }

    function delete($id=''){
        if($id != ''){
            if ($this->spb_model->isSaveDelete($id))
                    $result = $this->spb_model->DeleteOnDb($id);
            else
                    $result = false;
            if ($result){
                    echo json_encode(array('success'=>true, 'haha'=>''));
            } else {
                    echo json_encode(array('msg'=>'Data tidak bisa dihapus karena sudah digunakan sebagai referensi data lainnya.', 'data'=> ''));
            }
        }
    }

	public function print_serahterima($id){
		$this->load->library('cezpdf');	
		
		$header = $this->serahterima_ppk_model->selectById($id);
		$rowData= $this->serahterima_ppk_model->easyGridDetail(2,$header->tipe,$id);;
		if (isset($rowData)){
			foreach ($rowData as $r){
				
				$row[] = array($r[0],$r[1],$r[2],$r[3],$r[4],$r[5]);
			}
		}
		
		$data[]=array('label'=>'Nomor','separator'=>":",'value' => $header->nomor);
		$data[]=array('label'=>'Tanggal','separator'=>":",'value' =>$this->utility->ourFormatDate2($header->tanggal));
		$data[]=array('label'=>'Tipe','separator'=>":",'value' => ($header->tipe=="serah"?'Serah ke PPK':'Terima dari PPK'));
		$data[]=array('label'=>'Keterangan','separator'=>":",'value' =>$header->keterangan);
		
		$columnHeader = array("<b>No</b>","<b>No.Urut</b>","<b>Tanggal</b>","<b>Bidang</b>","<b>Kategori</b>","<b>Jumlah</b>");
		//,"<b>Untuk Pembayaran</b>","<b>Tujuan Pembayaran</b>"
        $pdf = new $this->cezpdf($paper='A4',$orientation='potrait');
        $pdf->ezSetCmMargins(1,1,1,1);
        $pdf->selectFont( APPPATH."libraries/fonts/Helvetica.afm" );

		
        $pdf->ezText('<u><b>Bukti Serah Terima Dokumen ke PPK</b></u>',12,array('justification'=>'center'));
		

        $pdf->ezText('');
        $pdf->ezText('');
       
	
		$pdf->ezTable($data,array('label'=>'Type','separator'=>':','value'=>'<i>Alias</i>'),'',array('showHeadings'=>0,'shaded'=>0,'showLines'=>0,'maxWidth'=>550, 'xPos' => 40,'xOrientation' => 'right'));
		$pdf->ezText('');
		$pdf->ezText('  <b>Daftar Dokumen Yang Diserahterimakan ke PPK</b>',10,array('justification'=>'left'));	
        $pdf->ezText('');
		$pdf->ezTable($row,$columnHeader,'',array('showHeadings'=>1,'shaded'=>0,'showLines'=>1,'maxWidth'=>530, 'xPos' => 40,'xOrientation' => 'right','cols'=>array(
			'1'=>array('width'=>90),
			'2'=>array('width'=>90),
			'3'=>array('width'=>150),	
			'4'=>array('width'=>100),
			'5'=>array('width'=>70,"justification"=>"right")
		)));
		$pdf->ezSetY(200);
        $footer[]=array('label'=>'','value'=>'');
        $footer[]=array('label'=>'','value'=>'');
        $footer[]=array('label'=>'','value'=>'');
        $footer[]=array('label'=>'','value'=>'');
        $footer[]=array('label'=>'','value'=>'');
        $footer[]=array('label'=>$header->penyerah,'value'=>$header->penerima);
		$pdf->ezTable($footer,array('label'=>'Yang Menyerahkan','value'=>'Penerima'),'',array('showHeadings'=>1,'shaded'=>0,'showLines'=>1, 'xPos' => 250,'xOrientation' => 'right','colGap' => 5 ,'cols'=>array(
								'label'=>array("width"=>150,'justification'=>'center'),
								'value'=>array("width"=>150,'justification'=>'center'))
								));
        //halaman 
        $pdf->ezStartPageNumbers(480,10,8,'right','Tgl.Cetak '.date('d-m-Y H:n:s'),1);//.'  Hal. {PAGENUM} dari {TOTALPAGENUM}',1);
		
       
		
        $opt['Content-Disposition'] = "SerahTerima.pdf";
        $pdf->ezStream($opt);
	}
	
	
    public function pdf($tipeapproval,$periode_awal,$periode_akhir,$bidang,$kategori,$nomor,$tipeperiode){
        $this->load->library('cezpdf');	
		 $periodeawal = $this->utility->ourDeFormatSQLDate($periode_awal);
		$periodeakhir = $this->utility->ourDeFormatSQLDate($periode_akhir);
        
        $pdfdata = $this->serahterima_ppk_model->easyGrid(2,$tipeapproval,$periodeawal,$periodeakhir,$bidang,$kategori,$nomor,$tipeperiode);
	//	var_dump($pdfdata);die;
        if (count($pdfdata)==0){
                echo "Data Tidak Tersedia";
                return;
        }
        //$pdfdata = $pdfdata->rows;
        $pdfhead = array('No.','No.Urut','Tanggal','Bidang','Kategori','Jumlah');
        $pdf = new $this->cezpdf($paper='A4',$orientation='landscape');
        $pdf->ezSetCmMargins(1,1,1,1);
        $pdf->selectFont( APPPATH."libraries/fonts/Helvetica.afm" );
//	$pdf->ezText('Biroren Kemenhub',8,array('left'=>'1'));
		switch ($tipeapproval){
			case "draft" : $purposeTitle = "Pengajuan SPBY"; break;
			case "verifikasi" : $purposeTitle = "SPBY Yang Harus di Verifikasi"; break;
			case "penguji" : $purposeTitle = "SPBY Yang Akan di Periksa oleh Pejabat Penguji "; break;
			default :$purposeTitle = "Serah/Terima dokumen SPBY ke PPK";
		}
        $pdf->ezText('Daftar '.$purposeTitle,12,array('left'=>'1'));
        $pdf->ezText('Periode : '.$periode_awal." s.d. ".$periode_akhir,12,array('left'=>'1'));
//	if (($filtahun != null)&&($filtahun != "-1"))
        //$pdf->ezText('Tahun 2012',12,array('left'=>'1'));
//	$pdf->ezText('Tahun 2012',12,array('left'=>'1'));
        // if (($file1 != null)&&($file1 != "-1"))
                // $pdf->ezText($this->eselon1_model->getNamaE1($file1),12,array('left'=>'1'));
        $pdf->ezText('');
			 $pdf->ezText('________________________________________________________________________________________________________________');
			 
        //halaman 
        $pdf->ezStartPageNumbers(650,10,8,'right','Tgl.Cetak '.date('d-m-Y H:n:s').'  Hal. {PAGENUM} dari {TOTALPAGENUM}',1);
		
        $optionsHeader = array(
                'showLines' => 0,
                'showHeadings' => 1,
                'fontSize' => 10,
                'rowGap' => 1,
                'shaded' => 0,
                'colGap' => 5,
                'xPos' => 40,
                'xOrientation' => 'right',
                        'cols'=>array(
                         0=>array('justification'=>'center','width'=>30),
                         1=>array('width'=>90),
                         2=>array('width'=>80),
                         3=>array('width'=>250),
                         4=>array('width'=>100),
                         5=>array('width'=>100,'justification'=>'right')),
                'width'=>'520'
        );
		$options = array(
                'showLines' => 2,
                'showHeadings' => 1,
                'fontSize' => 10,
                'rowGap' => 1,
                'shaded' => 0,
                'colGap' => 5,
                'xPos' => 40,
                'xOrientation' => 'right',
                        'cols'=>array(
                         0=>array('justification'=>'center','width'=>30),
                         1=>array('width'=>90),
                         2=>array('width'=>80),
                         3=>array('width'=>250),
                         4=>array('width'=>100),
                         5=>array('width'=>100,'justification'=>'right')),
                'width'=>'520'
        );
		foreach ($pdfdata as $header){
			$pdf->ezText('No.Serah/Terima: '.$header[2].'    Tanggal: '.$header[1].'     Tipe: '.($header[3]=="serah"?'Serah ke PPK    ':'Terima dari PPK').'   Keterangan: '.$header[4],12,array('left'=>'1'));
			$detail = $this->serahterima_ppk_model->easyGridDetail(2,$header[3],$header[5]);
			 $pdf->ezText('');
			$pdf->ezTable( $detail, $pdfhead, NULL, $options );
			 $pdf->ezText('______________________________________________________________________________________________________________________________________');
			 
			 // $pdf->setLineStyle(1);
			  // $y=$pdf->ezGetDy()-10;
			 // $r=$pdf->ez['pageWidth']-$pdf->ez['rightMargin'];
			 // $pdf->line($pdf->ez['leftMargin'],$y,$r,$y);
			 //$pdf->ezHR(1);
		}
			//$pdf->ezTable( $pdfdata, $pdfhead, NULL, $options );
		
        $opt['Content-Disposition'] = "SerahTerimaPPPK.pdf";
        $pdf->ezStream($opt);
    }

    public function excel($tipeapproval,$periode_awal,$periode_akhir,$bidang,$kategori,$nomor,$tipeperiode){
		$periodeawal = $this->utility->ourDeFormatSQLDate($periode_awal);
		$periodeakhir = $this->utility->ourDeFormatSQLDate($periode_akhir);
        echo  $this->spb_model->easyGrid(3,$tipeapproval,$periodeawal,$periodeakhir,$bidang,$kategori,$nomor,$tipeperiode);
		
    }
	
	
	public function print_tolak_old($id){
		$row=$this->rpt_spb_ditolak_model->selectById($id);
		$data['nomor'] = $row->nomor;
		$data['tanggal'] = $this->utility->ourFormatDate2($row->tanggal);
		$data['keterangan'] = $row->keterangan;
		
		
		$this->load->view('transaksi/bukti_tolak_print_v',$data);
		//$this->edit($id,true);
	}
	

}
?>